//
//  AddressProvinceCityController.m
//  testAddress
//
//  Created by kinglian_iOS_000 on 2018/1/21.
//  Copyright © 2018年 AiShan. All rights reserved.
//

#import "AddressProvinceCityController.h"

#import "KAddressRequest.h"


#import "AddressProvinceCityModel.h"



static NSString *const kProvinceCellId = @"_provinceCellId_";

@interface AddressProvinceCityController () {
    NSDictionary *singleProvinceDict_;
}

@property (nonatomic,copy) NSString * parentId;

@property (nonatomic, assign) AddressVCType addressVCType;

@property (nonatomic, strong) NSArray *allProvinceArray;

@property (nonatomic,copy) NSString * titleName;

@property (nonatomic,assign) AddressVCType lastVCType;

@property (nonatomic,strong) AddressProvinceCityResultModel *resultModel;


@end

@implementation AddressProvinceCityController

- (instancetype)initWithResultModel:(AddressProvinceCityResultModel *)resultModel AddressVCType:(AddressVCType)addressVCType parentId:(NSString *)parentId titleName:(NSString *)titleName lastVCType:(AddressVCType)lastVCType{
    if (self = [super init]) {
        if (resultModel == nil) {
            AddressProvinceCityResultModel * newResultModel = [[AddressProvinceCityResultModel alloc]init];
            self.resultModel = newResultModel;
        }else{
            self.resultModel = resultModel;
        }
        
        self.addressVCType = addressVCType;
        self.parentId = parentId;
        self.lastVCType = lastVCType;
        if (titleName.length>0) {
            self.navigationItem.title = titleName;
            self.titleName = titleName;
        }else{
            self.navigationItem.title = @"所在地";
        }
        
        [self getListData];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kProvinceCellId];
    self.tableView.tableFooterView = [[UIView alloc]init];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allProvinceArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kProvinceCellId];
    
    AddressProvinceCityModel * model =  self.allProvinceArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
 
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        AddressProvinceCityModel * model =  self.allProvinceArray[indexPath.row];
    MJWeakSelf
    
    switch (self.addressVCType) {
        case AddressVCTypeProvince:
            
            if (self.lastVCType == AddressVCTypeProvince) {
                if (self.addressCompleted) {
                    
                    self.resultModel.province = model.name;
                    self.resultModel.provinceId = model.id;
                    
                    self.addressCompleted(self.resultModel);
                }
            }else{
                
                AddressProvinceCityController * city = [[AddressProvinceCityController alloc]initWithResultModel:self.resultModel AddressVCType:AddressVCTypeCity parentId:model.id titleName:model.name lastVCType:self.lastVCType];
                [self.navigationController pushViewController:city animated:YES];
                
                [city setAddressCompleted:^(AddressProvinceCityResultModel *resultModel) {
                    
                    
                    
                    if (weakSelf.addressCompleted) {
                        self.resultModel.province = model.name;
                        self.resultModel.provinceId = model.id;
                        self.resultModel.city = resultModel.city;
                        self.resultModel.area = resultModel.area;
                        self.resultModel.cityId = resultModel.cityId;
                        self.resultModel.areaId = resultModel.areaId;
                        weakSelf.addressCompleted(self.resultModel);
                        
                        NSArray *vcs = self.navigationController.childViewControllers;
                        if (self.lastVCType == AddressVCTypeArea) {
                            [weakSelf.navigationController popToViewController:vcs[vcs.count - 4] animated:YES];
                        }else if (self.lastVCType == AddressVCTypeCity){
                            [weakSelf.navigationController popToViewController:vcs[vcs.count - 3] animated:YES];
                        }else{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        }
                        
                    }
                
                }];
            }
            
            
            break;
            
        case AddressVCTypeCity:
            
            if (self.lastVCType == AddressVCTypeCity) {
                
                
                
                if (self.addressCompleted) {
                    
                    self.resultModel.province = self.titleName;
                    self.resultModel.city = model.name;
                    self.resultModel.provinceId = self.parentId;
                    self.resultModel.cityId = model.id;
                    
                    self.addressCompleted(self.resultModel);
                }
            }else{
                
                AddressProvinceCityController * area = [[AddressProvinceCityController alloc]initWithResultModel:self.resultModel AddressVCType:AddressVCTypeArea parentId:model.id titleName:model.name lastVCType:self.lastVCType];
                
                [self.navigationController pushViewController:area animated:YES];
                
                [area setAddressCompleted:^(AddressProvinceCityResultModel *resultModel) {
                    
                    if (weakSelf.addressCompleted) {
                        self.resultModel.province = self.titleName;
                        self.resultModel.city = model.name;
                        self.resultModel.area = resultModel.area;
                        
                        self.resultModel.provinceId = self.parentId;
                        self.resultModel.cityId = model.id;
                        self.resultModel.areaId = resultModel.areaId;
                        
                        weakSelf.addressCompleted(resultModel);
                       
                        
                    }
                    
                    
                }];
            
            }
            
            
            break;
            
        case AddressVCTypeArea:
            
            if (self.addressCompleted) {
                NSString *city = self.titleName;
                NSString *area = model.name;
                
                self.resultModel.city = self.titleName;
                self.resultModel.area = model.name;
                self.resultModel.cityId = self.parentId;
                self.resultModel.areaId = model.id;
                
                self.addressCompleted(self.resultModel);
                
            }
            
            
            break;
            
        default:
            break;
    }
    
}



-(void)getListData{

    MJWeakSelf
    if (self.addressVCType == AddressVCTypeProvince) {
        
        KAddressRequest * request = [[KAddressRequest alloc]initGetProvinceCityListWithDept:@"1" parentId:@""];
        
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            AddressProvinceCityListModel * listModel = [AddressProvinceCityListModel modelWithJSON:baseModel.data];
            weakSelf.allProvinceArray = listModel.cateViews;
            [weakSelf.tableView reloadData];

        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
            
        }];
        

    }else if(self.addressVCType == AddressVCTypeCity){
        
        KAddressRequest * request = [[KAddressRequest alloc]initGetProvinceCityListWithDept:@"2" parentId:self.parentId];
        MJWeakSelf
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            AddressProvinceCityListModel * listModel = [AddressProvinceCityListModel modelWithJSON:baseModel.data];
            weakSelf.allProvinceArray = listModel.cateViews;
            [weakSelf.tableView reloadData];
            
        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
            
        }];
        
    }else if(self.addressVCType == AddressVCTypeArea){
        
        KAddressRequest * request = [[KAddressRequest alloc]initGetProvinceCityListWithDept:@"3" parentId:self.parentId];
        MJWeakSelf
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            AddressProvinceCityListModel * listModel = [AddressProvinceCityListModel modelWithJSON:baseModel.data];
            weakSelf.allProvinceArray = listModel.cateViews;
            [weakSelf.tableView reloadData];
            
        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
            
        }];
        
    }

}

@end


